﻿using Medicine.Entity;
using Medicine.Service;
using Medicine.View;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Medicine
{
    public partial class CheckRecord : Form
    {
        private RecordService rs = new RecordService();
        public CheckRecord()
        {
            InitializeComponent();
            flush(null);
        }
        //查询
        private void button1_Click(object sender, EventArgs e)
        {
            flush(this.textBox1.Text.Trim());
        }
        //增加
        private void button2_Click(object sender, EventArgs e)
        {
            AddRecord ar = new AddRecord();
            ar.Show();
        }
        //删除
        private void button3_Click(object sender, EventArgs e)
        {
            if (this.listView1.CheckedItems.Count != 0)
            {
                foreach (ListViewItem lv in this.listView1.CheckedItems)
                {
                    int id= int.Parse(lv.SubItems[0].Text);
                    rs.delete(id);
                }
                flush(null);
            }
        }
        //刷新数据
        private void button4_Click(object sender, EventArgs e)
        {
            flush(null);
        }
        //刷新操作
        private void flush(String name)
        {
            List<RecordDetail> med_list = rs.queryAll(name);
            this.listView1.Items.Clear();
            foreach (RecordDetail med in med_list)//遍历查询出来的数据
            {
                ListViewItem lv = new ListViewItem();
                lv.Text = med.Id.ToString();//设置第一行显示的数据
                //绑定剩余列的数据
                lv.SubItems.Add(med.Med_name);
                lv.SubItems.Add(med.Med_price.ToString());
                lv.SubItems.Add(med.User_id.ToString());
                lv.SubItems.Add(med.Time);
                lv.SubItems.Add(med.Address);
                lv.SubItems.Add(med.Remark);

                //一定记得行数据创建完毕后添加到列表中
                listView1.Items.Add(lv);
            }
        }
    }
}
